//
//  ICloudTable.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/13.
//

import Foundation
import GRDB

protocol IDbTable : FetchableRecord, PersistableRecord {
    func doCustomDBExcluded() -> [String?: Any?] //自定义排除（只排除leancloud的字段上传，json解析需要CodingKeys白名单） 反射
}

extension IDbTable {
    //执行数据库排除
    func doDBExcluded(to container: inout PersistenceContainer) {
        doCustomDBExcluded().forEach {
            let key = $0.key ?? ""
            let value = $0.value
            if let value = value as? any DatabaseValueConvertible {
                container[key] = value
            } else {
                if let value = value as? any Codable {
                    container[key] = toJson(value)
                } else {
                    container[key] = nil
                }
            }
        }
    }
}
